﻿using BCCommon;
using BCDto.Common.FAQ;
using BCService.Common.FAQ;
using Microsoft.AspNetCore.Mvc;
using System;

namespace BCTMSRESTful.Controllers.Anonymous
{
    [Route("api/v1/[controller]")]
    public class FaqController : BCTMSRESTfulAPIController
    {
        private readonly IFaqService faqService;

        public FaqController(IFaqService faqService)
        {
            this.faqService = faqService;
        }

        /// <summary>
        /// 查询常见问题列表（分页）
        /// </summary>
        /// <param name="faqId">常见问题ID</param>
        /// <param name="title">常见问题标题</param>
        /// <param name="keyWord">关键字</param>
        /// <param name="start">开始时间</param>
        /// <param name="end">结束时间</param>
        /// <param name="pageIndex">当前页码</param>
        /// <param name="pageSize">每页数据条数</param>
        /// <response code="200">查询常见问题列表</response>
        /// <response code="401">请求需要认证或者认证信息已过期。</response>
        /// <returns></returns>
        [HttpGet("[action]")]
        [ProducesResponseType(typeof(PageableList<FAQDto>), 200)]
        public IActionResult QueryFAQ(
            [FromQuery(Name = "faq_id")]long? faqId,
            [FromQuery(Name = "title")]string title,
            [FromQuery(Name = "key_word")]string keyWord,
            [FromQuery(Name = "start")]DateTime? start,
            [FromQuery(Name = "end")]DateTime? end,
            [FromQuery(Name = "index")]int pageIndex = 1,
            [FromQuery(Name = "size")]int pageSize = 10)
        {
            return this.Ok(this.faqService.QueryFAQ(faqId, title, keyWord, start, end, false, false, pageIndex, pageSize));
        }

        /// <summary>
        /// 新增常见问题
        /// </summary>
        /// <param name="RequestDto">常见问题请求模型</param>
        /// <response code="201">新增常见问题</response>
        /// <response code="401">请求需要认证或者认证信息已过期。</response>
        /// <returns></returns>
        [HttpPost]
        [ProducesResponseType(typeof(FAQDto), 201)]
        public IActionResult Post([FromBody]FAQRequestDto RequestDto)
        {
            var result = this.faqService.NewAdd(RequestDto.Title, RequestDto.Question, RequestDto.ReplyEmail);
            return this.Created(string.Format("/api/v1/QueryFAQ?faq_id={0}", result.FaqId), result);
        }
    }
}